<template>
<view class="container">
 <block v-if="!orderList">
    <view class="none">没有相关订单</view>
 </block>
 <block v-else>
    <view class="cartContainer" v-for="(list, index) in orderList" :key="index">
      <view class="cartShopName">
        <text>订单号：{{list.belongOrderNo}}</text>
        <!-- <text class='color'>{{list.easyStatusStr}}</text> -->
      </view>
      <view class="cartItems" :data-id="list.id" @tap="lookMore">
        <view class="itemCheckbox">
          <image :src="list.itemIcon"></image>
        </view>
        <view class="item_infomation">
          <view class="item_infomation_name">{{list.itemName}}</view>
           <view class="item_infomation_name1" v-if="list.measures">{{list.measures}}</view>
          
          <view class="item_infomation_money">
            <text :style="'color:' + setting.platformSetting.defaultColor + ';'">单价:￥ {{list.itemPrice}} x{{list.itemCount}}</text>
          </view>
        </view>
      </view>

      <view class="item_infomation_nums">
      <!-- 已完成 隐藏“申请退货”，显示“已退几件”-->
        <block v-if="list.orderStatus ==6">
          <text class="flexCenter butnForTuiKuan" :data-orderNo="list.id" :data-backCount="list.backCount" :data-itemCount="list.itemCount" @tap="tuikuan" :style="'color:' + setting.platformSetting.defaultColor + ';'">已退{{list.backCount}}件</text>
        </block>
        <!--已完成 隐藏“申请退货”，显示“已退几件”  end-->
        <block v-else> 
          <block v-if="list.backItem == 0 || list.backItem ==8">
          <text class="flexCenter butnForTuiKuan" :data-orderNo="list.id" :data-backCount="list.backCount" :data-itemCount="list.itemCount" @tap="tuikuan" :style="'color:' + setting.platformSetting.defaultColor + ';'">已退{{list.backCount}}件</text>
            <text class="flexCenter butnForTuiKuan" :data-orderNo="list.id" :data-backCount="list.backCount" :data-itemCount="list.itemCount" @tap="tuikuan" :style="'color:#fff;background:' + setting.platformSetting.defaultColor + ';'">申请退款</text>
          </block>
          <block v-if="list.backItem == 1">
            <text class="flexCenter butnForTuiKuan" :style="'color:' + setting.platformSetting.defaultColor">退款处理中</text>
          </block>

          <block v-if="list.backItem == 2">
            <text class="flexCenter butnForTuiKuan" :data-id="list.id" @tap="backWuLiu" :style="'color:#fff;background:' + setting.platformSetting.defaultColor + ';'">填写发货单</text>
            <text class="flexCenter butnForTuiKuan" :style="'color:' + setting.platformSetting.defaultColor">已同意申请</text>
          </block>

          <block v-if="list.backItem == 6">
            <text class="flexCenter butnForTuiKuan" :style="'color:' + setting.platformSetting.defaultColor">已填写发货单</text>
          </block>

          <block v-if="list.backItem == 7">
            <text class="flexCenter butnForTuiKuan" :style="'color:' + setting.platformSetting.defaultColor">商家已收到货</text>
          </block>

          <block v-if="list.backItem == 3">
            <text class="flexCenter butnForTuiKuan" :style="'color:' + setting.platformSetting.defaultColor">退款成功</text>
            </block>
          <block v-if="list.backItem == 5">
            <text class="flexCenter butnForTuiKuan" :data-orderNo="list.id" :data-backcount="list.backcount" :data-itemcount="list.itemcount" @tap="tuikuan" :style="'color:#fff;background:' + setting.platformSetting.defaultColor + ';'">申请退款</text>
            <text class="flexCenter butnForTuiKuan" :style="'color:' + setting.platformSetting.defaultColor">申请被拒绝</text>
          </block>
        </block>
      </view>
    </view>
 </block>
  
</view>
</template>

<script>
import publicFun from '../../wxcomponents/public/publicFun.wxs'

export default {
  data() {
    return {
      orderList: [],
      IfbackTo: false,
      listPage: {
        page: 1,
        pageSize: 0,
        totalSize: 0,
        curpage: 1
      },
      setting: "",
      loginUser: ""
    };
  },

  components: {},
  props: {},

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getOrderList();
    this.GloOption = options;
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.setData({
      setting: getApp().globalData.setting
    });
    this.setData({
      loginUser: getApp().globalData.loginUser
    });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (this.IfbackTo) {
      this.orderList = [];
      this.listPage.page = 1;
      this.getOrderList(this.GloOption);
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.orderList = [];
    this.listPage.page = 1;
    this.getOrderList(this.GloOption, 1);
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;

    if (that.listPage.totalSize > that.listPage.curPage * that.listPage.pageSize) {
      that.listPage.page++;
      this.getOrderList(that.GloOption);
    }
  },
  methods: {
    tuikuan: function (e) {
      var orderNo = e.currentTarget.dataset.orderno;
      wx.navigateTo({
        url: '/pages/back_item_detail/index?orderItemId=' + orderNo
      });
      this.setData({
        IfbackTo: true
      });
    },

    /* 去详情页面 */
    lookMore: function (e) {
      var id = e.currentTarget.dataset.id;
      wx.navigateTo({
        url: '/pages/back_item_more/index?orderItemId=' + id
      });
    },
    backWuLiu: function (e) {
      var id = e.currentTarget.dataset.id;
      wx.navigateTo({
        url: '/pages/back_item_wuliu/index?shopBackAmountId=' + id
      });
      this.setData({
        IfbackTo: true
      });
    },

    /* 获取订单列表 */
    getOrderList: function (options, fresh) {
      if (!getApp().globalData.checkIfLogin()) {
        return;
      }

      var customIndex = getApp().globalData.AddClientUrl("/get_back_item_list.html");
      var that = this; // wx.showLoading({
      //   title: 'loading'
      // })

      getApp().globalData.showToastLoading('loading', true);
      wx.request({
        url: customIndex.url + '&page=' + that.listPage.page,
        header: getApp().globalData.header,
        success: function (res) {
          console.log('-----------get_back_item_list--------');
          console.log(res.data);
          that.listPage.pageSize = res.data.pageSize;
          that.listPage.curPage = res.data.curPage;
          that.listPage.totalSize = res.data.totalSize;
          let dataArr = that.orderList;

          if (fresh) {
            dataArr = [];
          }

          dataArr = dataArr.concat(res.data.result);

          if (!res.data.result || res.data.result.length == 0) {
            that.setData({
              orderList: null
            });
          } else {
            that.setData({
              orderList: dataArr
            });
          }

          wx.hideLoading();
        },
        fail: function (res) {
          wx.hideLoading();
          getApp().globalData.loadFail();
        }
      });
    },
    setData: function (obj) {
      let that = this;
      let keys = [];
      let val, data;
      Object.keys(obj).forEach(function (key) {
        keys = key.split('.');
        val = obj[key];
        data = that.$data;
        keys.forEach(function (key2, index) {
          if (index + 1 == keys.length) {
            that.$set(data, key2, val);
          } else {
            if (!data[key2]) {
              that.$set(data, key2, {});
            }
          }

          data = data[key2];
        });
      });
    }
  }
};
</script>
<style>
@import "./index.css";
</style>